package io.framework.modules.water.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.modules.water.dao.WaterCityRiverDao;
import io.framework.modules.water.dto.WaterCityRiverDTO;
import io.framework.modules.water.entity.WaterCityRiverEntity;
import io.framework.modules.water.service.WaterCityRiverService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 重点治理城镇内河（湖）水质状况
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-25
 */
@Service
public class WaterCityRiverServiceImpl extends CrudServiceImpl<WaterCityRiverDao, WaterCityRiverEntity, WaterCityRiverDTO> implements WaterCityRiverService {

    @Override
    public QueryWrapper<WaterCityRiverEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<WaterCityRiverEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public void deleteInfoByDate(String month) {

        baseDao.deleteInfoByDate(month);
    }

    @Override
    public Boolean checkData(String month) {
        Integer count = baseDao.checkData(month);
        return count > 0 ? true : false;
    }

    @Override
    public WaterCityRiverDTO getWaterInfoById(Map<String, Object> params) {

        WaterCityRiverEntity entity = baseDao.getWaterInfoById(params);

        return ConvertUtils.sourceToTarget(entity, WaterCityRiverDTO.class);
    }

    @Override
    public PageData<WaterCityRiverDTO> pageList(Map<String, Object> params) {
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
        String orderField = jsonObject.getString("orderField");
        String order = jsonObject.getString("order");
        String time = jsonObject.getString("time");
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        QueryWrapper<WaterCityRiverEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(time)) {
            queryWrapper.eq("date_format(month,'%Y-%m')", time);
        }
        Page<WaterCityRiverEntity> p = new Page<>(page, limit);
        if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) {
            if (order.equals("desc")) {
                queryWrapper.orderByDesc(orderField);
            } else {
                queryWrapper.orderByAsc(orderField);
            }
        }
        IPage<WaterCityRiverEntity> ip = baseDao.selectPage(p, queryWrapper);
        List<WaterCityRiverDTO> dtoList = JSONArray.parseArray(JSONArray.toJSONString(ip.getRecords()), WaterCityRiverDTO.class);
        return new PageData<WaterCityRiverDTO>(dtoList, ip.getTotal());
    }
}
